﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Workflow
//  File name:	ActivityInstance.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;

namespace Smart.Workflow
{
	/// <summary>
	/// Activity instance (base class)
	/// </summary>
	[Serializable]
	public class ActivityInstance
	{
		public ProcessInstance Parent;

		public string Name;
		public int Index;

		public string Executor;
		public string Outcome;
		public string Comments;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:Smart.Workflow.ActivityInstance"/> class.
		/// </summary>
		public ActivityInstance(ProcessInstance parent, string name, int index)
		{
			Name = name;
			Index = index;
			Parent = parent;
		}

		/// <summary>
		/// Returns values for rules service
		/// </summary>
		public virtual void FillRulesValues(Dictionary<string, object> values)
		{
			values["InstanceName"] = Name;
			values["InstanceIndex"] = Index;

			values["Executor"] = Executor;
			values["Outcome"] = Outcome;
			values["Comments"] = Comments;
		}

		/// <summary>
		/// Perform action from rules on Workflow
		/// </summary>
		public virtual bool PerformAction(string actionName, string[] actionArgs, out bool handled)
		{
			if (Parent != null)
				return Parent.PerformAction(actionName, actionArgs, out handled);

			switch (actionName.ToLower())
			{
				case "setoutcome":
					handled = true;
					Outcome = actionArgs[0];
					return true;

				case "setcomments":
					handled = true;
					Comments = actionArgs[0];
					return true;

				case "addcomment":
					handled = true;
					Comments += actionArgs[0];
					return true;
			}

			handled = false;
			return false;
		}

		/// <summary>
		/// Execute Workflow rule condition function
		/// </summary>
		public virtual bool ExecuteFunction(string functionName, string[] functionArgs, out bool handled)
		{
			if (Parent != null)
				return Parent.ExecuteFunction(functionName, functionArgs, out handled);

			handled = false;
			return false;
		}
	}
}